package com.boo.service;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import vn.com.hoasua.database.smng.LinkMng;
import vn.com.hoasua.database.sobject.LinkPost;

import android.app.Activity;

import com.boo.baseinterface.IAfterRequestResponseJsonObject;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;

/**
 * Get user service - authentication service
 * 
 * @author ThuanHM <thuanhm@vnext.vn>
 * @since 2013/12/27
 * */
public class ProductService extends BaseService {
	private IAfterRequestResponseJsonObject context;

	public ProductService(Activity parentActivity,
			IAfterRequestResponseJsonObject response) {
		super(parentActivity);
		this.context = response;
	}

	public ProductService(Activity parentActivity) {
		super(parentActivity);
	}

	public void getData(final String url){
		this.get(parentActivity,url, new JsonHttpResponseHandler() {
			@Override
			public void onSuccess(JSONObject response) {
				context.doAfterPostObjectResponse(response);
				setCache(url, response.toString());
			}

			@Override
			public void onFailure(Throwable error) {
				error.printStackTrace();
			}
		});
	}

	public void getCategori(String url,
			JsonHttpResponseHandler callBack) {
		this.get(parentActivity,url, callBack);
	}

	/**
	 * Get unread message count
	 * 
	 * @param url
	 * @param jsonParams
	 */
	public void getUnreadInfo(final String url, JSONObject jsonParams) {

		try {
			StringEntity entity = new StringEntity(jsonParams.toString());
			// execute post request
			this.post(url, entity, new JsonHttpResponseHandler() {
				@Override
				public void onSuccess(JSONObject response) {
//					Log.d("receive", "recei " + response);
					context.doAfterPostObjectResponse(response);
				}

				@Override
				public void onFailure(Throwable error) {
					error.printStackTrace();
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}